/**
 * @file ajoutPanel.c
 * @brief Fichier source du module ajoutPanel.
 *
 * @author Rachid Delory, Mamadou Keita, Florian Bernard
 */

/* *********************************************************************

                       Librairies de ajoutPanel

********************************************************************* */

#include "ajoutPanel.h"

/* *********************************************************************

                        Fonctions                                      

********************************************************************* */
gboolean textChangedTitre(GtkWidget *widget,
		 GdkEvent  *event,
		 gpointer   user_data)
{
  AjoutPane *pane = user_data;
  if(pane->arbre == NULL)
    {
      if(pane->oeuvre != NULL)
	{
	  setTitre_Oeu(pane->oeuvre, gtk_entry_get_text(GTK_ENTRY(widget)));
	  setMaj_Oeu(pane->oeuvre, 1);
	}
    }
  else
    {
      setTitre_Reg(getInformation(pane->arbre), gtk_entry_get_text(GTK_ENTRY(widget)));
      setMaj_Reg(getInformation(pane->arbre), 1);
    }
  return FALSE;
  (void)event;
}

void commentaireChanged(GtkTextBuffer *textBuffer, gpointer user_data)
{
  GtkTextIter start, end;
  AjoutPane *pane = user_data;
  gtk_text_buffer_get_end_iter(textBuffer, &end);
  gtk_text_buffer_get_start_iter(textBuffer, &start);
  
  if(pane->arbre == NULL)
    {
      if(pane->oeuvre != NULL)
	{
	  setCommentaire_Oeu(pane->oeuvre, gtk_text_buffer_get_text(textBuffer, &start, &end, TRUE));
	  setMaj_Oeu(pane->oeuvre, 1);
	}
    }
  else
    {
      setCommentaire_Reg(getInformation(pane->arbre), gtk_text_buffer_get_text(textBuffer, &start, &end, TRUE));
      setMaj_Reg(getInformation(pane->arbre), 1);
    }
}

void typeChanged(GtkWidget *widget, gpointer user_data)
{
  AjoutPane *pane = user_data;
  gint id = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
  switch(id)
    {
    case AUCUN_OEU:
      gtk_widget_set_visible(pane->boxFilm, FALSE);
      gtk_widget_set_visible(pane->boxMusique, FALSE);
      gtk_widget_set_visible(pane->boxLivre, FALSE);
      setType_Oeu(pane->oeuvre, AUCUN_OEU);
      setMaj_Oeu(pane->oeuvre, 1);
      break;
    case FILM_OEU:
      gtk_widget_set_visible(pane->boxFilm, TRUE);
      gtk_widget_set_visible(pane->boxMusique, FALSE);
      gtk_widget_set_visible(pane->boxLivre, FALSE);
      setType_Oeu(pane->oeuvre, FILM_OEU);
      setMaj_Oeu(pane->oeuvre, 1);
      break;
    case LIVRE_OEU:
      gtk_widget_set_visible(pane->boxFilm, FALSE);
      gtk_widget_set_visible(pane->boxMusique, FALSE);
      gtk_widget_set_visible(pane->boxLivre, TRUE);
      setType_Oeu(pane->oeuvre, LIVRE_OEU);
      setMaj_Oeu(pane->oeuvre, 1);
      break;
    case MUSIQUE_OEU:
      gtk_widget_set_visible(pane->boxFilm, FALSE);
      gtk_widget_set_visible(pane->boxMusique, TRUE);
      gtk_widget_set_visible(pane->boxLivre, FALSE);
      setType_Oeu(pane->oeuvre, MUSIQUE_OEU);
      setMaj_Oeu(pane->oeuvre, 1);
      break;
    default:
      setType_Oeu(pane->oeuvre, AUCUN_OEU);
      setMaj_Oeu(pane->oeuvre, 1);
      break;
    }
}

void publicChanged(GtkWidget *widget, gpointer user_data)
{
  AjoutPane *pane = user_data;
  gint id = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
  setPublic_Oeu(pane->oeuvre, id);
  setMaj_Oeu(pane->oeuvre, 1);
}

char * setImage(AjoutPane *pane, char *filename, char *nom)
{
  GdkPixbuf *pixbuf;
  GError *error = NULL;
  char *fichier = NULL;
  pixbuf = gdk_pixbuf_new_from_file(filename, &error);
  
  if(!error)
    {
      char *chemin = "data/illustration/";
      char num[5];
      int i = 0;
      GdkPixbuf *pixbufMini = NULL;

      pixbufMini = gdk_pixbuf_scale_simple(pixbuf, 60, 40, GDK_INTERP_NEAREST);
      gtk_image_set_from_pixbuf(GTK_IMAGE(pane->image), pixbufMini);
      num [0] = '\0';
      if(nom)
	{
	  while(fichier == NULL)
	    {
	      char *tmp = NULL;
	      sprintf(num, "%d", i);
	      tmp = (char *) malloc ((strlen(num) + strlen(nom) + 5) *sizeof(char));
	      strcpy(tmp, nom);
	      strcat(tmp, num);
	      strcat(tmp, ".jpg");
	      fichier = g_build_filename(chemin, tmp, NULL);
	      free(tmp);
	      if(g_file_test(fichier, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) == TRUE)
		{
		  free(fichier);
		  fichier = NULL;
		}
	      i++;
	    }
	  gdk_pixbuf_save (pixbuf, fichier, "jpeg", &error, "quality", "100", NULL);
	}
    }
  else
    gtk_image_set_from_stock(GTK_IMAGE(pane->image), GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_LARGE_TOOLBAR);
  return fichier;
}

void imageChanged(GtkButton *btn, gpointer data)
{
  GtkWidget *dialog;
  AjoutPane *pane = data;
  dialog = gtk_file_chooser_dialog_new ("Sélection de l'image", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
  if(gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
    {
      char * filename;
      char *realfilename = NULL;
      filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));

      if(pane->arbre == NULL)
	{
	  if(pane->oeuvre != NULL)
	    {
	      realfilename = setImage(pane, filename, getTitre_Oeu(pane->oeuvre));
	      setIllustration_Oeu(pane->oeuvre, realfilename);
	      setMaj_Oeu(pane->oeuvre, 1);
	    }
	}
      else
	{
	  realfilename = setImage(pane, filename, getTitre_Reg(getInformation(pane->arbre)));
	  setIllustration_Reg(getInformation(pane->arbre), realfilename);
	  setMaj_Reg(getInformation(pane->arbre), 1);
	  
	}
      if(realfilename)
	free(realfilename);
      g_free(filename);
    }
  gtk_widget_destroy(dialog);
  (void)btn;
}

void textViewChanged(GtkTextBuffer *textBuffer, gpointer user_data)
{
  GtkTextIter start, end;
  AjoutPane *pane = user_data;
  gtk_text_buffer_get_end_iter(textBuffer, &end);
  gtk_text_buffer_get_start_iter(textBuffer, &start);
  
  if(pane->arbre == NULL)
    {
      if(pane->oeuvre != NULL)
	{
	  if(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeSynopsis)) == textBuffer)
	    {
	      if(getType_Oeu(pane->oeuvre) == FILM_OEU)
		{
		  setSynopsis_Fil(getMedia_Oeu(pane->oeuvre), gtk_text_buffer_get_text(textBuffer, &start, &end, TRUE));
		  setMaj_Fil(getMedia_Oeu(pane->oeuvre), 1);
		}
	    }
	  else if(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeParole)) == textBuffer)
	    {
	      setParoles_Mus(getMedia_Oeu(pane->oeuvre), gtk_text_buffer_get_text(textBuffer, &start, &end, TRUE));
	      setMaj_Mus(getMedia_Oeu(pane->oeuvre), 1);
	    }
	}
    }
}

void dateChanged(GtkCalendar *calendar, gpointer data)
{
  AjoutPane *pane = data;
  guint jour, mois, annee;
  Date *date = NULL;

  gtk_calendar_get_date(calendar, &annee, &mois, &jour);
  date = creer_Dat();
  if(date)
    {
      setMois_Dat(date, mois + 1);
      setJour_Dat(date, jour);
      setAnnee_Dat(date, annee);
      setMaj_Dat(date, 1);
    }
  if(calendar == GTK_CALENDAR(pane->dateSortie))
    {
      setDateSortie_Oeu(pane->oeuvre, date);
      setMaj_Oeu(pane->oeuvre, 1);
    }
  else if(calendar == GTK_CALENDAR(pane->dateBR))
    {
      setDateBlueRay_Fil(getMedia_Oeu(pane->oeuvre), date);
      setMaj_Fil(getMedia_Oeu(pane->oeuvre), 1);
    }
  else if(calendar == GTK_CALENDAR(pane->dateDvd))
    {
      setDateDvd_Fil(getMedia_Oeu(pane->oeuvre), date);
      setMaj_Fil(getMedia_Oeu(pane->oeuvre), 1);
    }
  detruire_Dat(&date);
}

void productionChanged(GtkWidget *widget,
			gpointer   user_data)
{
  AjoutPane *pane = user_data;
  if(widget == pane->dateProd)
    {
      Date *date = creer_Dat();
      setAnnee_Dat(date, gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)));
      setMaj_Dat(date, 1);
      setDateProduction_Fil(getMedia_Oeu(pane->oeuvre), date);
      setMaj_Fil(getMedia_Oeu(pane->oeuvre), 1);
      detruire_Dat(&date);
    }
  else if(widget == pane->entreeVolume)
    {
      setVolume_Liv(getMedia_Oeu(pane->oeuvre), gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)));
      setMaj_Mus(getMedia_Oeu(pane->oeuvre), 1);
    }
  else if(widget == pane->entreeTaille)
    {
      setTaille_Liv(getMedia_Oeu(pane->oeuvre), gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)));
      setMaj_Liv(getMedia_Oeu(pane->oeuvre), 1);
    }
  else if(widget == pane->entreeNbPage)
    {
      setNbPage_Liv(getMedia_Oeu(pane->oeuvre), gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)));
      setMaj_Liv(getMedia_Oeu(pane->oeuvre), 1);
    }
  else if(widget == pane->entreeDuree)
    {
      setTemps_Tem(getDuree_Mus(getMedia_Oeu(pane->oeuvre)), gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)));
      setMaj_Mus(getMedia_Oeu(pane->oeuvre), 1);
      setMaj_Tem(getDuree_Mus(getMedia_Oeu(pane->oeuvre)), 1);
    }
  else if(widget == pane->entreePiste)
    {
      setPiste_Mus(getMedia_Oeu(pane->oeuvre), gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget)));
      setMaj_Mus(getMedia_Oeu(pane->oeuvre), 1);
    }
}

gboolean isbnChanged(GtkWidget *widget,
		 GdkEvent  *event,
		 gpointer   user_data)
{
  AjoutPane *pane = user_data;
  if(pane->arbre == NULL)
    {
      if(pane->oeuvre != NULL)
	{
	  setIsbn_Liv(getMedia_Oeu(pane->oeuvre), gtk_entry_get_text(GTK_ENTRY(widget)));
	  setMaj_Liv(getMedia_Oeu(pane->oeuvre), 1);
	}
    }
  return FALSE;
  (void)event;
}



void activerSignaux(AjoutPane *pane)
{
  pane->hTitre = g_signal_connect(pane->entreeTitre, "key-release-event", G_CALLBACK(textChangedTitre), pane);
  pane->hType = g_signal_connect(pane->comboType, "changed", G_CALLBACK(typeChanged), pane);
  pane->hComm = g_signal_connect(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeCommentaire)), "changed", G_CALLBACK(commentaireChanged), pane);
  pane->hImage = g_signal_connect(pane->btnImage, "clicked", G_CALLBACK(imageChanged), pane);
  pane->hSynopsis = g_signal_connect(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeSynopsis)), "changed", G_CALLBACK(textViewChanged), pane);
  pane->hPublic = g_signal_connect(pane->comboPublic, "changed", G_CALLBACK(publicChanged), pane);
  pane->hSortieJour = g_signal_connect(pane->dateSortie, "day-selected", G_CALLBACK(dateChanged), pane);
  pane->hBR = g_signal_connect(pane->dateBR, "day-selected", G_CALLBACK(dateChanged), pane);
  pane->hDvd = g_signal_connect(pane->dateDvd, "day-selected", G_CALLBACK(dateChanged), pane);
  pane->hProd = g_signal_connect(pane->dateProd, "value-changed", G_CALLBACK(productionChanged), pane);
  pane->hVolume = g_signal_connect(pane->entreeVolume, "value-changed", G_CALLBACK(productionChanged), pane);
  pane->hTaille = g_signal_connect(pane->entreeTaille, "value-changed", G_CALLBACK(productionChanged), pane);
  pane->hNbPage = g_signal_connect(pane->entreeNbPage, "value-changed", G_CALLBACK(productionChanged), pane);
  pane->hPiste = g_signal_connect(pane->entreePiste, "value-changed", G_CALLBACK(productionChanged), pane);
  pane->hDuree = g_signal_connect(pane->entreeDuree, "value-changed", G_CALLBACK(productionChanged), pane);
  pane->hISBN = g_signal_connect(pane->entreeISBN, "key-release-event", G_CALLBACK(isbnChanged), pane);
  pane->hParole = g_signal_connect(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeParole)), "changed", G_CALLBACK(textViewChanged), pane);
}

void desactiverSignaux(AjoutPane *pane)
{
  g_signal_handler_disconnect(pane->entreeTitre, pane->hTitre);
  g_signal_handler_disconnect(pane->comboType, pane->hType);
  g_signal_handler_disconnect(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeCommentaire)), pane->hComm);
  g_signal_handler_disconnect(pane->btnImage, pane->hImage);
  g_signal_handler_disconnect(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeSynopsis)), pane->hSynopsis);
  g_signal_handler_disconnect(pane->comboPublic, pane->hPublic);
  g_signal_handler_disconnect(pane->dateSortie, pane->hSortieJour);
  g_signal_handler_disconnect(pane->dateBR, pane->hBR);
  g_signal_handler_disconnect(pane->dateDvd, pane->hDvd);
  g_signal_handler_disconnect(pane->dateProd, pane->hProd);
  g_signal_handler_disconnect(pane->entreeVolume, pane->hVolume);
  g_signal_handler_disconnect(pane->entreeTaille, pane->hTaille);
  g_signal_handler_disconnect(pane->entreeNbPage, pane->hNbPage);
  g_signal_handler_disconnect(pane->entreePiste, pane->hPiste);
  g_signal_handler_disconnect(pane->entreeDuree, pane->hDuree);
  g_signal_handler_disconnect(pane->entreeISBN, pane->hISBN);
  g_signal_handler_disconnect(gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeParole)), pane->hParole);
}

AjoutPane * creer_GAPan()
{
  GtkWidget *vbox;
  GtkWidget *labelTitre, *labelType, *labelPublic, *label;
  GtkWidget *hbox;
  AjoutPane *pane;
  GtkWidget *btn;
  GtkWidget *scroll;
  pane = (AjoutPane *) malloc (1*sizeof(AjoutPane));
  pane->arbre = NULL;
  pane->oeuvre = NULL;
  pane->scroll = gtk_scrolled_window_new(NULL, NULL);
  vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(pane->scroll), vbox);
  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  /* Partie générale */
  pane->image = gtk_image_new();

  gtk_box_pack_start(GTK_BOX(vbox), pane->image, FALSE, FALSE, 1);
  pane->btnImage = gtk_button_new_with_label("Image");
  gtk_box_pack_start(GTK_BOX(vbox), pane->btnImage, FALSE, FALSE, 1);
  labelTitre = gtk_label_new("Titre");
  gtk_box_pack_start(GTK_BOX(hbox), labelTitre, FALSE, FALSE, 1);
  pane->entreeTitre = gtk_entry_new();

  gtk_box_pack_start(GTK_BOX(hbox), pane->entreeTitre, FALSE, FALSE, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 1);

  btn  = gtk_expander_new("Genres");
  gtk_box_pack_start(GTK_BOX(vbox), btn, FALSE, FALSE, 1);
  pane->genre = creer_GLga(pane);
  gtk_container_add(GTK_CONTAINER(btn), pane->genre);
  gtk_widget_set_size_request(pane->genre, -1, 200);

  /* Partie oeuvre */
  pane->boxOeuvre = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), pane->boxOeuvre, FALSE, FALSE, 1);
  btn = gtk_expander_new("Personnes");
  gtk_box_pack_start(GTK_BOX(pane->boxOeuvre), btn, FALSE, FALSE, 1);
  pane->personne = creer_GLpa(pane);
  gtk_container_add(GTK_CONTAINER(btn), pane->personne);
  gtk_widget_set_size_request(pane->personne, -1, 200);
  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxOeuvre), hbox, FALSE, FALSE, 1);

  labelType = gtk_label_new("Type de l'oeuvre");
  gtk_box_pack_start(GTK_BOX(hbox), labelType, FALSE, FALSE, 1);
  pane->comboType = gtk_combo_box_text_new();
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboType), NULL, "Aucun");
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboType), NULL, "Film");
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboType), NULL, "Musique");
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboType), NULL, "Livre");
  gtk_box_pack_start(GTK_BOX(hbox), pane->comboType, FALSE, FALSE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxOeuvre), hbox, FALSE, FALSE, 1);
  labelPublic = gtk_label_new("Public");
  gtk_box_pack_start(GTK_BOX(hbox), labelPublic, FALSE, FALSE, 1);
  pane->comboPublic = gtk_combo_box_text_new();
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboPublic), NULL, getLibelle_Pub(AUCUN_PUB));
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboPublic), NULL, getLibelle_Pub(TOUS_PUBLIC_PUB));
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboPublic), NULL, getLibelle_Pub(AVERTISSEMENT_PUB));
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboPublic), NULL, getLibelle_Pub(MOINS_12_PUB));
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboPublic), NULL, getLibelle_Pub(MOINS_16_PUB));
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboPublic), NULL, getLibelle_Pub(MOINS_18_NON_X_PUB));
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboPublic), NULL, getLibelle_Pub(MOINS_18_X_PUB));

  gtk_box_pack_start(GTK_BOX(hbox), pane->comboPublic, FALSE, FALSE, 1);

  btn  = gtk_expander_new("Date de sortie");
  gtk_box_pack_start(GTK_BOX(pane->boxOeuvre), btn, FALSE, FALSE, 1);
  pane->dateSortie = gtk_calendar_new();
  gtk_container_add(GTK_CONTAINER(btn), pane->dateSortie);


  /* Partie film */
  pane->boxFilm = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxOeuvre), pane->boxFilm, FALSE, FALSE, 1);
  
  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxFilm), hbox, FALSE, FALSE, 1);

  pane->comboMetrage = gtk_combo_box_text_new();
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboMetrage), NULL, "Court");
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboMetrage), NULL, "Moyen");
  gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(pane->comboMetrage), NULL, "Long");
  gtk_box_pack_start(GTK_BOX(hbox), pane->comboMetrage, FALSE, FALSE, 1);
  label = gtk_label_new("métrage");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);


  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxFilm), hbox, FALSE, FALSE, 1);

  label = gtk_label_new("Date de production");
  gtk_box_pack_start(GTK_BOX(hbox),  label, FALSE, FALSE, 1);

  pane->dateProd = gtk_spin_button_new_with_range(-1000, 3000, 1);
  gtk_box_pack_start(GTK_BOX(hbox), pane->dateProd, FALSE, FALSE, 1);

  btn  = gtk_expander_new("Date de sortie blue-ray");
  gtk_box_pack_start(GTK_BOX(pane->boxFilm), btn, FALSE, FALSE, 1);
  pane->dateBR = gtk_calendar_new();
  gtk_container_add(GTK_CONTAINER(btn), pane->dateBR);

  btn  = gtk_expander_new("Date de sortie DVD");
  gtk_box_pack_start(GTK_BOX(pane->boxFilm), btn, FALSE, FALSE, 1);
  pane->dateDvd = gtk_calendar_new();
  gtk_container_add(GTK_CONTAINER(btn), pane->dateDvd);  
  
  btn = gtk_expander_new ("Synopsis");
  gtk_box_pack_start(GTK_BOX(pane->boxFilm), btn, FALSE, FALSE, 1);
  scroll = gtk_scrolled_window_new(NULL, NULL);
  g_object_set (scroll, "shadow-type", GTK_SHADOW_IN, NULL);
  gtk_container_add(GTK_CONTAINER(btn), scroll);
  pane->entreeSynopsis = gtk_text_view_new();
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), pane->entreeSynopsis);

  /* Partie de livre */
  pane->boxLivre = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxOeuvre), pane->boxLivre, FALSE, FALSE, 1);
  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxLivre), hbox, FALSE, FALSE, 1);

  label = gtk_label_new("Numéro du volume");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  pane->entreeVolume = gtk_spin_button_new_with_range(0, 500, 1);
  gtk_box_pack_start(GTK_BOX(hbox), pane->entreeVolume, FALSE, FALSE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxLivre), hbox, FALSE, FALSE, 1);
 
  label = gtk_label_new("Taille du volume");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  pane->entreeTaille = gtk_spin_button_new_with_range(0, 5000, 1);
  gtk_box_pack_start(GTK_BOX(hbox), pane->entreeTaille, FALSE, FALSE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxLivre), hbox, FALSE, FALSE, 1);
  label = gtk_label_new("Nombre de page");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  pane->entreeNbPage =  gtk_spin_button_new_with_range(0, 30000, 1);
  gtk_box_pack_start(GTK_BOX(hbox), pane->entreeNbPage, FALSE, FALSE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxLivre), hbox, FALSE, FALSE, 1);
  label = gtk_label_new("ISBN");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  pane->entreeISBN = gtk_entry_new();
  gtk_box_pack_start(GTK_BOX(hbox), pane->entreeISBN, FALSE, FALSE, 1);

  /* Partie de la musique */
  pane->boxMusique = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxOeuvre), pane->boxMusique, FALSE, FALSE, 1);
  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxMusique), hbox, FALSE, FALSE, 1);

  label = gtk_label_new("Numéro de la piste");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  pane->entreePiste = gtk_spin_button_new_with_range(0, 100, 1);
  gtk_box_pack_start(GTK_BOX(hbox), pane->entreePiste, FALSE, FALSE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(pane->boxMusique), hbox, FALSE, FALSE, 1);

  label = gtk_label_new("Durée");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  pane->entreeDuree = gtk_spin_button_new_with_range(0, 1440, 1);
  gtk_box_pack_start(GTK_BOX(hbox), pane->entreeDuree, FALSE, FALSE, 1);

  btn = gtk_expander_new("Paroles");
  gtk_box_pack_start(GTK_BOX(pane->boxMusique), btn, FALSE, FALSE, 1);
  scroll = gtk_scrolled_window_new(NULL, NULL);
  g_object_set (scroll, "shadow-type", GTK_SHADOW_IN, NULL);
  gtk_container_add(GTK_CONTAINER(btn), scroll);
  pane->entreeParole = gtk_text_view_new();
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), pane->entreeParole);

  btn = gtk_expander_new ("Commentaire");
  gtk_box_pack_start(GTK_BOX(vbox), btn, FALSE, FALSE, 1);
  scroll = gtk_scrolled_window_new(NULL, NULL);
  g_object_set (scroll, "shadow-type", GTK_SHADOW_IN, NULL);
  gtk_container_add(GTK_CONTAINER(btn), scroll);
  pane->entreeCommentaire = gtk_text_view_new();
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), pane->entreeCommentaire);

  /* Signals */
  activerSignaux(pane);
  
  return pane;
}

GtkWidget * getContainer_GAPan(AjoutPane * pane)
{
  return pane->scroll;
}

void setInformationRegroupement_GAPan(AjoutPane * pane, Arbre *arbre)
{
  char *text;
  GtkTextBuffer *buffer;

  desactiverSignaux(pane);
  pane->arbre = arbre;
  pane->oeuvre = NULL;
  text = getTitre_Reg(getInformation(arbre));
  
  gtk_entry_set_text(GTK_ENTRY(pane->entreeTitre), (text == NULL ? "" : text));
  text = getCommentaire_Reg(getInformation(arbre));
  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeCommentaire));
  gtk_text_buffer_set_text (buffer, (text == NULL ? "" : text), (text == NULL ? 0 : strlen(text)));
  text = getIllustration_Reg(getInformation(arbre));
  if(text)
    {
      setImage(pane, text, NULL);
    }
  else
    gtk_image_set_from_stock(GTK_IMAGE(pane->image), GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_LARGE_TOOLBAR);
  gtk_widget_set_size_request(pane->image, 30, 20);
  gtk_widget_set_visible(pane->boxOeuvre, FALSE);
  checkGenre_LGga(getEnsembleGenre_Reg(getInformation(arbre)), pane->genre);

  activerSignaux(pane);
}
void setCalendar_GAPan(GtkCalendar *calendar, Date *date)
{
  if(date == NULL)
    {
      gtk_calendar_select_month(calendar, 11, 2011);
      gtk_calendar_select_day(calendar, 1);
    }
  else
    {
      gtk_calendar_select_month(calendar, (getMois_Dat(date) == 0 ? getMois_Dat(date) : getMois_Dat(date) - 1), getAnnee_Dat(date));
      gtk_calendar_select_day(calendar, getJour_Dat(date));
    }
}
void setInformationFilm_GAPan(AjoutPane * pane, Oeuvre *oeuvre)
{
  if(getType_Oeu(oeuvre) == FILM_OEU)
    {
      GtkTextBuffer *buffer;
      char *text;
      int annee;
      text = getSynopsis_Fil(getMedia_Oeu(oeuvre));

      buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeSynopsis));
      gtk_text_buffer_set_text (buffer, (text == NULL ? "" : text), (text == NULL ? 0 : strlen(text)));
      if(getDateProduction_Fil(getMedia_Oeu(oeuvre)) == NULL)
	{
	  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->dateProd), 2011);
	}
      else
	{
	  annee = getAnnee_Dat(getDateProduction_Fil(getMedia_Oeu(oeuvre)));
	  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->dateProd), annee);
	}
      setCalendar_GAPan(GTK_CALENDAR(pane->dateBR), getDateBlueRay_Fil(getMedia_Oeu(oeuvre)));
      setCalendar_GAPan(GTK_CALENDAR(pane->dateDvd), getDateDvd_Fil(getMedia_Oeu(oeuvre)));
      gtk_combo_box_set_active(GTK_COMBO_BOX(pane->comboMetrage), getMetrage_Fil(getMedia_Oeu(oeuvre)));
    }
}

void setInformationLivre_GAPan(AjoutPane *pane)
{
  Livre *livre = getMedia_Oeu(pane->oeuvre);
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreeVolume), getVolume_Liv(livre));
  gtk_entry_set_text(GTK_ENTRY(pane->entreeISBN), (getIsbn_Liv(livre) ? getIsbn_Liv(livre) : ""));
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreeTaille), getTaille_Liv(livre));
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreeNbPage), getNbPage_Liv(livre));
}

void setInformationMusique_GAPan(AjoutPane *pane)
{
  Musique *musique = getMedia_Oeu(pane->oeuvre);
  GtkTextBuffer *buffer;

  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreePiste), getPiste_Mus(musique));
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreeDuree), getTemps_Tem(getDuree_Mus(musique)));
  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeParole));
  gtk_text_buffer_set_text(buffer, (getParoles_Mus(musique) ? getParoles_Mus(musique) : ""),
			   (getParoles_Mus(musique) ? strlen(getParoles_Mus(musique)) : 0));
}

void setInformationNone_GAPan(AjoutPane *pane)
{
  GtkTextBuffer *buffer;
  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeSynopsis));
  gtk_text_buffer_set_text (buffer, "", 0);
  if(getDateProduction_Fil(getMedia_Oeu(pane->oeuvre)) == NULL)
    {
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->dateProd), 2011);
    }
  else
    {
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->dateProd), 2011);
    }
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreeVolume), 1);
  gtk_entry_set_text(GTK_ENTRY(pane->entreeISBN), "");
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreeTaille), 0);
  gtk_spin_button_set_value(GTK_SPIN_BUTTON(pane->entreeNbPage), 0);
  checkGenre_LGga(NULL, pane->genre);
  checkPersonne_GLpa(NULL, pane->personne);
}

void setInformationOeuvre_GAPan(AjoutPane * pane, Oeuvre *oeuvre)
{
  char *text;
  GtkTextBuffer *buffer;
  int i;

  desactiverSignaux(pane);
  pane->arbre = NULL;
  pane->oeuvre = oeuvre;
  text = getTitre_Oeu(oeuvre);

  gtk_entry_set_text(GTK_ENTRY(pane->entreeTitre), (text == NULL ? "" : text));
  text = getCommentaire_Oeu(oeuvre);

  buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pane->entreeCommentaire));
  gtk_text_buffer_set_text (buffer, (text == NULL ? "" : text), (text == NULL ? 0 : strlen(text)));

  text = getIllustration_Oeu(oeuvre);
  if(text)
    {
      setImage(pane, text, NULL);
    }
  else
    gtk_image_set_from_stock(GTK_IMAGE(pane->image), GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_LARGE_TOOLBAR);
  checkGenre_LGga(getEnsembleGenre_Oeu(oeuvre), pane->genre);
  checkPersonne_GLpa(getEnsembleCollaborateur_Oeu(oeuvre), pane->personne);

  switch(getType_Oeu(oeuvre))
    {
    case FILM_OEU:
      setInformationFilm_GAPan(pane, pane->oeuvre);
      break;
    case MUSIQUE_OEU:
      setInformationMusique_GAPan(pane);
      break;
    case LIVRE_OEU:
      setInformationLivre_GAPan(pane);
      break;
    default:
      setInformationNone_GAPan(pane);
      break;
    }
  activerSignaux(pane);
  i = getType_Oeu(oeuvre);
  gtk_combo_box_set_active(GTK_COMBO_BOX(pane->comboType), i);
  gtk_widget_set_visible(pane->boxOeuvre, TRUE);
}

void ajoutListenerTitre_GAPan(AjoutPane * pane, void *aArbre, gboolean (*fonc)())
{
  g_signal_connect(pane->entreeTitre, "key-release-event", G_CALLBACK(fonc), aArbre);
}

void ajoutListenerType_GAPan(AjoutPane * pane, void *aArbre, void (*fonc)())
{
  g_signal_connect(pane->comboType, "changed", G_CALLBACK(fonc), aArbre);
}
